using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using Commons;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Diagnostics;
using System.Collections;
using System.Xml.XPath;
using System.Collections.Generic;
using WPFBPMN.Converters;


namespace WPFBPMN
{
    /// <summary>
    /// InterID logic for App.xaml
    /// </summary>

    public partial class App : System.Windows.Application
    {
        public delegate void BPMNContextMenuShowing(object sender, List<string> arguments,ref object info);
        public event BPMNContextMenuShowing BPMNCMShowing;

        public App()
        {
            Application.Current.Resources.Add("BPMNBrushConverter", new BPMNBrushConverter());
            Application.Current.Resources.Add("StringToBoolConverter", new StringToBoolConverter());
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"..\WPFBPMN\BPMNShapeContextMenuNoID.xml");
            XmlElement element = xmlDoc.DocumentElement;
            int uniqueID = 1;
            AssignUniqueID(xmlDoc, element, ref uniqueID);
            xmlDoc.Save(@"..\WPFBPMN\BPMNShapeContextMenu.xml");

        }
        void AssignUniqueID(XmlDocument xmlDoc, XmlNode node, ref int id)
        {
            XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("ID");
            xmlAttribute.Value = "" + id++;
            node.Attributes.Append(xmlAttribute);
            foreach (XmlNode childNode in node.ChildNodes)
            {
                AssignUniqueID(xmlDoc, childNode, ref id);
            }
        }

        void CheckBoxItem_Click(object sender, RoutedEventArgs e)
        {
            if (BPMNCMShowing != null)
            {
                (sender as MenuItem).IsChecked =!(sender as MenuItem).IsChecked;
                List<string> arguments = new List<string>();
                arguments.Add("Locked");
                object obj = new object();
                BPMNCMShowing.Invoke(this, arguments,ref obj);

            }
        }

        void Item_Click(object sender, RoutedEventArgs e)
        {
            if (BPMNCMShowing != null)
            {
                List<string> arguments = new List<string>();
                arguments.Add(((sender as Control).Tag as XmlAttribute).Value.ToString());
                object obj = new object();
                BPMNCMShowing.Invoke(this, arguments, ref obj);
            }
        }


        void IconItem_Click(object sender, RoutedEventArgs e)
        {
            if (BPMNCMShowing != null)
            {
                string tag = ((sender as Control).Tag as XmlAttribute).Value.ToString();
                List<string> arguments = new List<string>();
                string fileName = (Application.Current.Resources["BPMNShapeContextMenuXml"] as XmlDataProvider).Source.ToString();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(@"..\WPFBPMN\" + fileName);
                XPathDocument doc = new XPathDocument(@"..\WPFBPMN\" + fileName);
                XPathNavigator nav = doc.CreateNavigator();
                XPathExpression expr = nav.Compile(String.Format("//*[@ID='{0}']", tag));
                XPathNodeIterator xPathIterator = nav.Select(expr);
                while (xPathIterator.MoveNext())
                {
                    XPathNavigator navigator = xPathIterator.Current;
                    XPathNodeIterator parents = navigator.SelectAncestors(XPathNodeType.All, true);
                    while (parents.MoveNext())
                    {
                        string Action = parents.Current.GetAttribute("Action", "");
                        if (Action != null && Action != String.Empty)
                        {
                            if (Action.ToLower().IndexOf("context") == -1)
                            {
                                arguments.Add(Action);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                arguments.Reverse();
                object obj = new object();
                BPMNCMShowing.Invoke(this, arguments,ref obj);
            }
        }
    }
}